package org.pearl.spring.demo.aop;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by TD on 2021/8/10
 */
@Configuration
public class AopConfig {

    // 拦截方法的SpEL表达式，这里表示拦截TestService.test()
    public static final String traceExecution = "execution(org.pearl.spring.demo.service.TestService.test())";

    /**
     * 对切点进行增强
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor() {
        // 创建拦截器
        MyMethodInterceptor interceptor = new MyMethodInterceptor();
        // 配置切面
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(traceExecution);
        // 配置增强类advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(interceptor);
        return advisor;
    }
}
